"""
UI样式模块 - 定义应用程序的样式和主题
"""

from PySide2.QtCore import QTimer, QPropertyAnimation, QEasingCurve, QParallelAnimationGroup
from PySide2.QtGui import QColor, QPalette, QLinearGradient
import random


class UIStyles:
    """UI样式管理类"""
    
    def __init__(self):
        self.is_dark_mode = False
        self.dynamic_timer = None
        self.current_gradient_index = 0
        
    def get_main_style(self, is_dark_mode=False, bg_animation_state=0):
        """获取主窗口样式 - 炫酷赛博朋克主题"""
        if is_dark_mode:
            # 夜间模式 - 深邃宇宙科技感（原版样式）
            # 使用动态背景
            backgrounds = [
                """qlineargradient(x1:0, y1:0, x2:1, y2:1,
                    stop:0 #0c0c0c, stop:0.2 #1a1a2e,
                    stop:0.4 #16213e, stop:0.6 #0f3460,
                    stop:0.8 #533483, stop:1 #2d1b69)""",
                """qlineargradient(x1:0, y1:0, x2:1, y2:1,
                    stop:0 #2d1b69, stop:0.2 #533483,
                    stop:0.4 #0f3460, stop:0.6 #16213e,
                    stop:0.8 #1a1a2e, stop:1 #0c0c0c)""",
                """qlineargradient(x1:0, y1:0, x2:1, y2:1,
                    stop:0 #16213e, stop:0.2 #0f3460,
                    stop:0.4 #533483, stop:0.6 #2d1b69,
                    stop:0.8 #0c0c0c, stop:1 #1a1a2e)""",
                """qlineargradient(x1:0, y1:0, x2:1, y2:1,
                    stop:0 #533483, stop:0.2 #2d1b69,
                    stop:0.4 #0c0c0c, stop:0.6 #1a1a2e,
                    stop:0.8 #16213e, stop:1 #0f3460)"""
            ]
            main_bg = backgrounds[bg_animation_state % len(backgrounds)]
            text_color = "#ffffff"  # 夜间模式使用白色文字，确保清晰可见
            secondary_text_color = "#b0c4de"  # 次要文字使用浅蓝灰色
            input_bg = "rgba(255, 255, 255, 0.08)"
            input_border = "rgba(138, 180, 248, 0.3)"
            input_focus_border = "#00d4ff"
            group_bg = """
                qlineargradient(x1:0, y1:0, x2:0, y2:1,
                    stop:0 rgba(255, 255, 255, 0.05),
                    stop:1 rgba(255, 255, 255, 0.02))
            """
            group_border = "rgba(0, 212, 255, 0.4)"
            group_title_bg = """
                qlineargradient(x1:0, y1:0, x2:1, y2:0,
                    stop:0 #00d4ff, stop:0.5 #667eea, stop:1 #764ba2)
            """
            progress_bg = "rgba(255, 255, 255, 0.05)"
            progress_border = "rgba(0, 212, 255, 0.3)"
            scroll_bg = "rgba(255, 255, 255, 0.05)"
            scroll_handle = "rgba(0, 212, 255, 0.4)"
            scroll_handle_hover = "#00d4ff"
            card_shadow = "rgba(0, 212, 255, 0.2)"
        else:
            # 日间模式 - 固定蓝色霓虹科技风
            # 使用固定的蓝色背景，不再动态切换
            main_bg = "#003366"  # 固定使用深蓝色（调暗）
            text_color = "#ffffff"  # 日间模式也使用白色文字，在深蓝背景上清晰可见
            secondary_text_color = "#e6f3ff"  # 次要文字使用浅蓝白色
            input_bg = "rgba(0, 0, 0, 0.4)"
            input_border = "rgba(0, 153, 204, 0.6)"
            input_focus_border = "#0099cc"
            group_bg = """
                qlineargradient(x1:0, y1:0, x2:0, y2:1,
                    stop:0 rgba(0, 153, 204, 0.1),
                    stop:1 rgba(204, 17, 119, 0.05))
            """
            group_border = "rgba(0, 153, 204, 0.6)"
            group_title_bg = """
                qlineargradient(x1:0, y1:0, x2:1, y2:0,
                    stop:0 #0099cc, stop:0.5 #cc1177, stop:1 #0088bb)
            """
            progress_bg = "rgba(0, 0, 0, 0.4)"
            progress_border = "rgba(0, 153, 204, 0.5)"
            scroll_bg = "rgba(0, 0, 0, 0.3)"
            scroll_handle = "rgba(0, 153, 204, 0.6)"
            scroll_handle_hover = "#0099cc"
            card_shadow = "rgba(0, 153, 204, 0.3)"

        # 精美现代化样式
        main_style = f"""
            QMainWindow {{
                background: {main_bg};
                font-family: 'Microsoft YaHei UI', 'SF Pro Display', 'Segoe UI', Arial, sans-serif;
            }}

            QLabel {{
                color: #ffffff;
                font-weight: 600;
                font-size: 11pt;
            }}

            /* 透明玻璃态输入框 */
            QLineEdit, QTextEdit {{
                background: rgba(255, 255, 255, 0.15);
                color: {text_color} !important;
                border: 2px solid rgba(0, 201, 255, 0.4);
                border-radius: 16px;
                padding: 16px 20px;
                min-height: 28px;
                min-width: 400px;
                font-size: 12pt;
                font-weight: 500;
                selection-background-color: {input_focus_border};
            }}

            QLineEdit:focus, QTextEdit:focus {{
                border: 2px solid #00c9ff;
                background: rgba(255, 255, 255, 0.2);
                color: {text_color} !important;
            }}

            QLineEdit:hover, QTextEdit:hover {{
                border: 2px solid rgba(0, 201, 255, 0.6);
                background: rgba(255, 255, 255, 0.18);
                color: {text_color} !important;
            }}

            QLineEdit::placeholder {{
                color: rgba(255, 255, 255, 0.6);
                font-style: italic;
                font-weight: 500;
            }}

            /* 炫酷进度条 */
            QProgressBar {{
                border: none;
                border-radius: 15px;
                background: rgba(0, 0, 0, 0.3);
                text-align: center;
                color: #ffffff;
                font-weight: 700;
                font-size: 12pt;
                min-height: 30px;
            }}

            QProgressBar::chunk {{
                background: qlineargradient(x1:0, y1:0, x2:1, y2:0,
                    stop:0 #00d4ff, stop:0.2 #667eea,
                    stop:0.5 #764ba2, stop:0.8 #f093fb, stop:1 #ff6b6b);
                border-radius: 13px;
                margin: 2px;
            }}

            /* 透明玻璃态分组框 */
            QGroupBox {{
                border: 2px solid rgba(0, 201, 255, 0.3);
                border-radius: 20px;
                margin: 25px 10px 15px 10px;
                background: rgba(255, 255, 255, 0.08);
                font-weight: 700;
                font-size: 14pt;
                padding-top: 15px;
                color: #ffffff;
            }}

            QGroupBox::title {{
                subcontrol-origin: margin;
                subcontrol-position: top center;
                padding: 12px 24px;
                background: qlineargradient(x1:0, y1:0, x2:1, y2:0,
                    stop:0 #00c9ff, stop:0.5 #667eea, stop:1 #764ba2);
                color: #6ca6fd;
                border-radius: 12px;
                font-weight: 800;
                font-size: 13pt;
                letter-spacing: 1.5px;
            }}

            /* 分组框内控件美化 */
            QGroupBox QLabel {{
                color: {text_color} !important;
                background: transparent;
                font-weight: 500;
            }}

            QGroupBox QLineEdit, QGroupBox QTextEdit {{
                color: {text_color} !important;
                background: rgba(255, 255, 255, 0.08);
                border: 2px solid rgba(255, 255, 255, 0.15);
                border-radius: 12px;
                padding: 12px 16px;
                min-width: 450px;
            }}

            /* 全局输入框透明化 */
            QLineEdit {{
                background: rgba(255, 255, 255, 0.08) !important;
                border: 2px solid rgba(255, 255, 255, 0.15) !important;
                border-radius: 12px !important;
                color: {text_color} !important;
                padding: 12px 16px !important;
                min-width: 450px !important;
            }}

            QLineEdit:focus {{
                background: rgba(255, 255, 255, 0.12) !important;
                border: 2px solid rgba(0, 212, 255, 0.5) !important;
            }}

            /* 全局文本框透明化 */
            QTextEdit {{
                background: rgba(255, 255, 255, 0.08) !important;
                border: 2px solid rgba(255, 255, 255, 0.15) !important;
                border-radius: 12px !important;
                color: {text_color} !important;
                padding: 12px !important;
                min-width: 450px !important;
                min-height: 120px !important;
            }}

            QGroupBox QLineEdit:focus, QGroupBox QTextEdit:focus {{
                background: rgba(255, 255, 255, 0.12);
                border: 2px solid {input_focus_border};
            }}

            QGroupBox QLineEdit:hover, QGroupBox QTextEdit:hover {{
                background: rgba(255, 255, 255, 0.1);
                border: 2px solid rgba(255, 255, 255, 0.25);
            }}

            /* 滚动条样式 */
            QScrollBar:vertical {{
                background: {scroll_bg};
                width: 12px;
                border-radius: 6px;
            }}

            QScrollBar::handle:vertical {{
                background: {scroll_handle};
                border-radius: 6px;
                min-height: 20px;
            }}

            QScrollBar::handle:vertical:hover {{
                background: {scroll_handle_hover};
            }}

            QScrollBar::add-line:vertical, QScrollBar::sub-line:vertical {{
                height: 0px;
            }}
        """

        return main_style
    
    def get_sidebar_style(self, is_dark_mode=False):
        """获取侧边栏样式"""
        if is_dark_mode:
            return """
                background: qlineargradient(x1:0, y1:0, x2:1, y2:1,
                    stop:0 #0c0c0c, stop:0.2 #1a1a2e,
                    stop:0.4 #16213e, stop:0.6 #0f3460,
                    stop:0.8 #533483, stop:1 #2d1b69);
                color: #ffffff;
                border-right: 4px solid qlineargradient(x1:0, y1:0, x2:0, y2:1,
                    stop:0 #00d4ff, stop:0.3 #667eea, stop:0.6 #764ba2, stop:1 #533483);
            """
        else:
            return """
                background: #003366;
                color: white;
                border-right: 4px solid #004488;
            """
    
    def get_title_frame_style(self):
        """获取标题区域样式（固定橙色渐变）"""
        return """
            QFrame {
                background: qlineargradient(x1:0, y1:0, x2:0, y2:1,
                    stop:0 #ff6b6b, stop:1 #ff8e53);
                border: none;
                border-radius: 15px;
                margin: 10px;
            }
        """
    
    def get_title_label_style(self):
        """获取标题文字样式"""
        return """
            color: #ffffff;
            font-weight: 700;
        """
    
    def get_version_label_style(self):
        """获取版本标签样式"""
        return """
            color: #ffffff;
            font-weight: 400;
            margin-top: 5px;
        """
    
    def get_menu_button_style(self):
        """获取菜单按钮样式"""
        return """
            QPushButton {
                background-color: transparent;
                color: #6ca6fd;
                border: none;
                text-align: left;
                padding: 18px 20px 18px 25px;
                font-size: 12pt;
                font-weight: 500;
                border-radius: 12px;
                margin: 2px 15px;
            }
            QPushButton:hover {
                background: qlineargradient(x1:0, y1:0, x2:1, y2:0,
                    stop:0 rgba(255, 255, 255, 0.15),
                    stop:1 rgba(255, 255, 255, 0.08));
                color: #6ca6fd;
                padding-left: 30px;
                border-left: 4px solid #00d4ff;
            }
            QPushButton:pressed {
                background: rgba(255, 255, 255, 0.2);
                padding-left: 35px;
            }
        """
    
    def get_active_menu_button_style(self):
        """获取激活状态的菜单按钮样式"""
        return """
            QPushButton {
                background: qlineargradient(x1:0, y1:0, x2:1, y2:0,
                    stop:0 rgba(0, 212, 255, 0.25),
                    stop:1 rgba(102, 126, 234, 0.15));
                color: #ffffff;
                border: none;
                text-align: left;
                padding: 18px 20px 18px 30px;
                font-size: 12pt;
                font-weight: 600;
                border-radius: 12px;
                margin: 2px 15px;
                border-left: 4px solid #00d4ff;
            }
            QPushButton:hover {
                background: qlineargradient(x1:0, y1:0, x2:1, y2:0,
                    stop:0 rgba(0, 212, 255, 0.35),
                    stop:1 rgba(102, 126, 234, 0.25));
                padding-left: 35px;
            }
        """
    
    def get_theme_toggle_style(self, is_dark_mode=False):
        """获取主题切换按钮样式"""
        if is_dark_mode:
            return """
                QPushButton {
                    background: qlineargradient(x1:0, y1:0, x2:1, y2:0,
                        stop:0 rgba(0, 212, 255, 0.2),
                        stop:1 rgba(102, 126, 234, 0.2));
                    color: #ffffff;
                    border: 2px solid rgba(0, 212, 255, 0.4);
                    border-radius: 15px;
                    padding: 12px 18px;
                    font-size: 11pt;
                    font-weight: 600;
                    margin: 8px 12px;
                    letter-spacing: 1px;
                }
                QPushButton:hover {
                    background: qlineargradient(x1:0, y1:0, x2:1, y2:0,
                        stop:0 rgba(0, 212, 255, 0.4),
                        stop:1 rgba(102, 126, 234, 0.4));
                    border: 2px solid #00d4ff;
                }
                QPushButton:pressed {
                    background: rgba(0, 212, 255, 0.6);
                }
            """
        else:
            return """
                QPushButton {
                    background: qlineargradient(x1:0, y1:0, x2:1, y2:0,
                        stop:0 rgba(255, 255, 255, 0.2),
                        stop:1 rgba(255, 255, 255, 0.1));
                    color: white;
                    border: 2px solid rgba(255, 255, 255, 0.3);
                    border-radius: 15px;
                    padding: 12px 18px;
                    font-size: 11pt;
                    font-weight: 600;
                    margin: 8px 12px;
                    letter-spacing: 1px;
                }
                QPushButton:hover {
                    background: qlineargradient(x1:0, y1:0, x2:1, y2:0,
                        stop:0 rgba(255, 255, 255, 0.4),
                        stop:1 rgba(255, 255, 255, 0.3));
                    border: 2px solid rgba(255, 255, 255, 0.6);
                }
                QPushButton:pressed {
                    background: rgba(255, 255, 255, 0.5);
                }
            """
    
    def get_input_style(self, is_dark_mode=False):
        """获取输入框样式"""
        text_color = "#ffffff" if is_dark_mode else "#ffffff"  # 两种模式都使用白色文字
        return f"""
            QLineEdit {{
                background: rgba(255, 255, 255, 0.08) !important;
                color: {text_color} !important;
                border: 2px solid rgba(255, 255, 255, 0.2);
                border-radius: 8px;
                padding: 12px 15px;
                font-size: 11pt;
                selection-background-color: rgba(0, 212, 255, 0.3);
            }}
            QLineEdit:focus {{
                border: 2px solid rgba(0, 212, 255, 0.6);
                background: rgba(255, 255, 255, 0.12) !important;
            }}
        """
    
    def get_button_style(self, style_type="default"):
        """获取按钮样式 - 霓虹赛博朋克风格"""
        if style_type == "primary":
            return """
                QPushButton {
                    background: qlineargradient(x1:0, y1:0, x2:1, y2:0,
                        stop:0 #ff00ff, stop:0.5 #00ffff, stop:1 #ff0080);
                    color: white;
                    border: 2px solid #ff00ff;
                    border-radius: 12px;
                    padding: 12px 24px;
                    font-size: 11pt;
                    font-weight: 600;
                    text-shadow: 0 0 10px rgba(255, 0, 255, 0.8);
                }
                QPushButton:hover {
                    background: qlineargradient(x1:0, y1:0, x2:1, y2:0,
                        stop:0 #ff44ff, stop:0.5 #44ffff, stop:1 #ff4488);
                    border: 2px solid #00ffff;
                    text-shadow: 0 0 15px rgba(0, 255, 255, 0.9);
                }
                QPushButton:pressed {
                    background: qlineargradient(x1:0, y1:0, x2:1, y2:0,
                        stop:0 #cc00cc, stop:0.5 #00cccc, stop:1 #cc0066);
                    border: 2px solid #ff44ff;
                }
            """
        elif style_type == "success":
            return """
                QPushButton {
                    background: qlineargradient(x1:0, y1:0, x2:1, y2:0,
                        stop:0 #48bb78, stop:1 #38a169);
                    color: white;
                    border: none;
                    border-radius: 8px;
                    padding: 12px 24px;
                    font-size: 11pt;
                    font-weight: 600;
                }
                QPushButton:hover {
                    background: qlineargradient(x1:0, y1:0, x2:1, y2:0,
                        stop:0 #38a169, stop:1 #2f855a);
                }
            """
        elif style_type == "danger":
            return """
                QPushButton {
                    background: qlineargradient(x1:0, y1:0, x2:1, y2:0,
                        stop:0 #f56565, stop:1 #e53e3e);
                    color: white;
                    border: none;
                    border-radius: 8px;
                    padding: 12px 24px;
                    font-size: 11pt;
                    font-weight: 600;
                }
                QPushButton:hover {
                    background: qlineargradient(x1:0, y1:0, x2:1, y2:0,
                        stop:0 #e53e3e, stop:1 #c53030);
                }
            """
        else:  # default
            return """
                QPushButton {
                    background: rgba(255, 255, 255, 0.1);
                    color: white;
                    border: 2px solid rgba(255, 255, 255, 0.2);
                    border-radius: 8px;
                    padding: 12px 24px;
                    font-size: 11pt;
                    font-weight: 500;
                }
                QPushButton:hover {
                    background: rgba(255, 255, 255, 0.2);
                    border: 2px solid rgba(255, 255, 255, 0.4);
                }
                QPushButton:pressed {
                    background: rgba(255, 255, 255, 0.3);
                }
            """
